package com.train.classeval.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.train.classeval.entity.EvalActivity;
import com.train.classeval.entity.OV.ShowActiveOV;
import com.train.classeval.entity.OV.ShowProActiveOV;
import com.train.classeval.entity.ProfessorEval;
import com.train.classeval.service.impl.EvalActivityServiceImpl;
import com.train.classeval.service.impl.ProfessorEvalServiceImpl;
import com.train.classeval.service.impl.ProfessorRatingServiceImpl;
import com.train.classeval.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2021-10-23
 */
@RestController
public class EvalActivityController {

    @Autowired
    private EvalActivityServiceImpl evalActivityService;

    @Autowired
    private ProfessorEvalServiceImpl professorEvalService;

    @Autowired
    private ProfessorRatingServiceImpl professorRatingService;

    /**
     * 添加活动的控制器
     * @param evalActivity
     * @param cid
     * @return
     */
    @PostMapping("/addEAT/{cid}")
    public JsonData addEAT(@RequestBody EvalActivity evalActivity, @PathVariable("cid") Integer cid) {
        //课程基本信息预先设置
        evalActivity.setCreateDate(LocalDateTime.now());
        evalActivity.setUpdateDate(LocalDateTime.now());
        evalActivity.setCalStatus(0);
        evalActivity.setEAStatus(1);
        evalActivity.setCourseId(cid);
        evalActivity.setRatedNum(0);
        evalActivity.setCalResult(0);
        evalActivity.setResultDesc("暂无备注");
        //随机选专家
        final List<Integer> list = professorEvalService.selectPro(evalActivity.getTotalNum());
        final boolean save = evalActivityService.save(evalActivity);
        for (Integer integer : list) {
            ProfessorEval professorEval = new ProfessorEval();
            professorEval.setActivityId(evalActivity.getId());
            professorEval.setProfessorId(integer);
            professorEval.setPEStatus(1);
            professorEval.setCreateDate(LocalDateTime.now());
            professorEval.setUpdateDate(LocalDateTime.now());
            //添加到专家和活动的关联表当中
            professorEvalService.save(professorEval);
        }
        if (save == true) {
            return JsonData.BuildSuccess("添加评价活动成功!");
        }
        return JsonData.BuildError("添加活动失败!");
    }

    /**
     * 查找所有活动
     * @return
     */
    @GetMapping("/findAllActive")
    public JsonData findAllActive() {
        List<ShowActiveOV> list = evalActivityService.findAllAct();
        //拿出所有的活动以后对活动状态进行识别
        //如果为0就是以及各个专家已经评完 需要算分，没有就原来的装进行就可以了
        for (ShowActiveOV showActiveOV : list) {
            if (showActiveOV.getEAStatus().equals("0")) {
                //算总分，更新calResult信息再重新装载
                Integer score = professorRatingService.score(showActiveOV.getId());
                Integer newScore = Integer.valueOf((int) (score * 0.1));
                //更新活动成绩
                Integer integer = evalActivityService.updateScore(showActiveOV.getId(), newScore);
            }
        }
        //更新完成以后查第二次
        List<ShowActiveOV> newList = evalActivityService.findAllAct();
        if (newList != null) {
            return JsonData.BuildSuccess(newList, "查询活动成功");
        }
        return JsonData.BuildError("查询活动失败");
    }

    /**
     * 展示当前专家要评价的活动
     * @param pid
     * @return
     */
    @GetMapping("/showProAct/{pid}")
    public JsonData showProAct(@PathVariable("pid") Integer pid) {
        final List<ShowProActiveOV> showProActiveOVS = evalActivityService.showProAct(pid);
        if (showProActiveOVS != null) {
            return JsonData.BuildSuccess(showProActiveOVS, "当前专家要评课程查询成功!");
        }
        return JsonData.BuildError("查询错误!");
    }

    /**
     * 查询某个具体的活动信息
     * @param aid
     * @return
     */
    @GetMapping("/findOneActive/{aid}")
    public JsonData findOneActive(@PathVariable("aid") Integer aid) {
        ShowActiveOV oneActive = evalActivityService.findOneActive(aid);
        if (oneActive != null) {
            return JsonData.BuildSuccess(oneActive, "查询成功!");
        }
        return JsonData.BuildError("查询失败!");
    }


    /**
     * 删除活动
     * @param aid
     * @return
     */
    @DeleteMapping("/deleteActive/{aid}")
    public JsonData deleteActive(@PathVariable("aid") Integer aid) {
        final boolean b = evalActivityService.removeById(aid);
        if (b == true) {
            return JsonData.BuildSuccess("删除成功!");
        }
        return JsonData.BuildError("删除失败！");
    }

}
